<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class CaseComService extends BaseService
{
    //公司

    //设计公司列表
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbCompany)
            ->where('del', 0)
            ->where('type', 3);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $cprefix = file_exists($v['logo']) ? '/' : '';
            $arys[] = [
                'id' => $v['id'],
                'cname' => $v['cname'],
                'cprefix' => $cprefix,
                'clogo' => file_exists($v['logo']) ? $v['logo'] : '',
                'address' => $v['address'] ? $v['address'] : '-',
                'uptime' => $v['uptime'] ? $v['uptime'] : '-',
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //添加
    public function add($pas)
    {
        $res = (array) $this->dbConn($this->dbCompany)
            ->where('del', 0)
            ->where('type', 3)
            ->where('cname', $pas['cname'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '存在同名公司');
        }
        $pas['uptime'] = $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbCompany)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    //修改
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbCompany)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbCompany)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, '修改失败');
        }
        return $this->toback();
    }

    //删除
    public function del($id)
    {
        $res = (array) $this->dbConn($this->dbCompany)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $up = $this->dbConn($this->dbCompany)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}